#ifndef WIRELESS_H
#define WIRELESS_H

#include <stdint.h>
#include <stdbool.h>
#define WIRELESS_8KEY      1
#define PROCS_PROTOCOL     0

#define CHUANZHOU_PROTOCOL 1

#if (WIRELESS_8KEY == 1)
extern uint8_t uc_rd_key01;
extern uint8_t uc_rd_key02;
extern uint8_t uc_rd_key03;
extern uint8_t uc_rd_key04;
extern uint8_t uc_rd_key05;
extern uint8_t uc_rd_key06;
extern uint8_t uc_rd_key07;
extern uint8_t uc_rd_key08;
extern uint8_t uc_rd_key_emorgency_stop;

#endif
// Common settings,Long Range Transceiver
#define REG_LR_OPMODE                                0x01 //Operating mode & LoRaTM / FSK selection
#define REG_LR_BANDSETTING                           0x04 //Frequency Deviation setting, Most Significant Bits
#define REG_LR_FRFMSB                                0x06 //RF Carrier Frequency, Most Significant Bits
#define REG_LR_FRFMID                                0x07 //RF Carrier Frequency, Intermediate Bits
#define REG_LR_FRFLSB                                0x08 //RF Carrier Frequency, Least Significant Bits
 // Tx settings
 //0x09,Selects PA output pin;0->RFO pin.Maximum power of +14dBm;1->PA_BOOST pin,Maximum power of +20dBm
#define REG_LR_PACONFIG                              0x09 
#define REG_LR_PARAMP                                0x0A 
#define REG_LR_OCP                                   0x0B 
 // Rx settings
#define REG_LR_LNA                                   0x0C //bit7~bit5:LNA gain setting;bit4~bit3:Low Frequency(RFI_LF) LNA current adjustment;bit1~bit0:High Frequency(RFI_HF) LNA current adjustment
 // LoRa registers
#define REG_LR_FIFOADDRPTR                           0x0D //SPI interface address pointer in FIFO data buffer
#define REG_LR_FIFOTXBASEADDR                        0x0E 
#define REG_LR_FIFORXBASEADDR                        0x0F 
#define REG_LR_FIFORXCURRENTADDR                     0x10 
#define REG_LR_IRQFLAGSMASK                          0x11 
#define REG_LR_IRQFLAGS                              0x12 //中断标志，接收中断，发送中断等
#define REG_LR_NBRXBYTES                             0x13 //Number of payload bytes of latest packet received
#define REG_LR_RXHEADERCNTVALUEMSB                   0x14 
#define REG_LR_RXHEADERCNTVALUELSB                   0x15 
#define REG_LR_RXPACKETCNTVALUEMSB                   0x16 
#define REG_LR_RXPACKETCNTVALUELSB                   0x17 
#define REG_LR_MODEMSTAT                             0x18 
#define REG_LR_PKTSNRVALUE                           0x19 
#define REG_LR_PKTRSSIVALUE                          0x1A 
#define REG_LR_RSSIVALUE                             0x1B 
#define REG_LR_HOPCHANNEL                            0x1C 
#define REG_LR_MODEMCONFIG1                          0x1D 
#define REG_LR_MODEMCONFIG2                          0x1E 
#define REG_LR_SYMBTIMEOUTLSB                        0x1F 
#define REG_LR_PREAMBLEMSB                           0x20 
#define REG_LR_PREAMBLELSB                           0x21 
#define REG_LR_PAYLOADLENGTH                         0x22 
#define REG_LR_PAYLOADMAXLENGTH                      0x23 
#define REG_LR_HOPPERIOD                             0x24 
#define REG_LR_FIFORXBYTEADDR                        0x25
#define REG_LR_MODEMCONFIG3                          0x26
#define REG_LR_FEIMSB                                0x28
#define REG_LR_FEIMIB                                0x29
#define REG_LR_FEILSB                                0x2A
#define REG_LR_LORADETECTOPTIMIZE                    0x31
#define REG_LR_INVERTIQ                              0x33
#define REG_LR_DETECTIONTHRESHOLD                    0x37

#define REG_LR_RAWTEMPERATURE                        0x3C //读取 raw temperature
 // end of documented register in datasheet
 // I/O settings
#define REG_LR_DIOMAPPING1                           0x40   //Mapping of pins DIO0 to DIO3
#define REG_LR_DIOMAPPING2                           0x41   //Mapping of pins DIO4 and DIO5, ClkOut frequency
 // Version
#define REG_LR_VERSION                               0x42
 // Additional settings
#define REG_LR_PLLHOP                                0x44
#define REG_LR_TCXO                                  0x4B
#define REG_LR_PADAC                                 0x4D
#define REG_LR_FORMERTEMP                            0x5B
#define REG_LR_BITRATEFRAC                           0x5D
#define REG_LR_AGCREF                                0x61
#define REG_LR_AGCTHRESH1                            0x62
#define REG_LR_AGCTHRESH2                            0x63
#define REG_LR_AGCTHRESH3                            0x64

#define GPIO_VARE_1                                  0X00
#define GPIO_VARE_2                                  0X00

#define RFLR_MODEMCONFIG2_SF_MASK                    0x0f
#define RFLR_MODEMCONFIG1_CODINGRATE_MASK            0xF1 
#define RFLR_MODEMCONFIG2_RXPAYLOADCRC_MASK          0xFB 
#define RFLR_MODEMCONFIG1_BW_MASK                    0x0F 
#define RFLR_MODEMCONFIG1_IMPLICITHEADER_MASK        0xFE 
#define RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK        0xfc
#define RFLR_MODEMCONFIG3_MOBILE_NODE_MASK           0xF7 


#define IRQN_TXD_Value                               0xF7
#define IRQN_RXD_Value                               0x9F
#define IRQN_CAD_Value                               0xFA
#define IRQN_SEELP_Value                             0xFF
#define PACKET_MIAX_Value                            0xff

/*!
* RegPaConfig
*/
#define RFLR_PACONFIG_PASELECT_MASK                 0x7F
#define RFLR_PACONFIG_PASELECT_PABOOST              0x80
#define RFLR_PACONFIG_PASELECT_RFO                  0x00 // Default

#define RFLR_OPMODE_MASK                            0xF8
#define RFLR_OPMODE_SLEEP                           0x00
#define RFLR_OPMODE_STANDBY                         0x01 // Default
#define RFLR_OPMODE_SYNTHESIZER_TX                  0x02
#define RFLR_OPMODE_TRANSMITTER                     0x03
#define RFLR_OPMODE_SYNTHESIZER_RX                  0x04
#define RFLR_OPMODE_RECEIVER                        0x05

/*!
 * Constant values need to compute the RSSI value
 */
#define RSSI_OFFSET_LF                              -164
#define RSSI_OFFSET_HF                              -157


typedef enum 
{
    Sleep = 0,
    Standby = 1,
    SynthesizerTx = 2,
    Transmit = 3,
    SynthesizerRx = 4,
    Receive = 5,
    ReceiveSingle = 6,
    CAD = 7,
    LoRaTransmit = 0x83,
    LoRaReceive = 0x85,
    LoRaReceiveSingle = 0x86,
}op_mode_t;
typedef enum{
   rxOpen,
   txOpen
}cmdpaType_t;
typedef enum{
   enOpen,        //片选打开
   enClose        //片选关闭
}cmdEntype_t;
#if (PROCS_PROTOCOL == 1)
typedef union rd_recv_data
{
    uint8_t buf[24];
    struct
    {
        uint16_t head;                 //头，            长度2个字节，开始字节-0
        uint16_t addr;                 //地址，          长度2个字节，开始字节-2
        uint16_t len;                  //长度，          长度2个字节，开始字节-4，长度是出去头和尾4个字节的数据
        uint16_t cmd;                  //命令，          长度2个字节，开始字节-6，0-心跳包命令，发送周期1S钟一次；1-数据包，发送周期100ms一次
        uint16_t data[2];              //数据，          长度2个字节，开始字节-8，这里仅仅只有9个按键的数据，因此只用2个字节表示
        uint16_t power_voltage;        //手持端电压，    长度2个字节，开始字节-12
        uint16_t rollingCounter;       //心跳计数，      长度2个字节，开始字节-14
        uint16_t power_off_left_timel; //剩余关机时间，  长度2个字节，开始字节-16
        uint16_t power_off_left_timeh; //剩余关机时间，  长度2个字节，开始字节-18
        uint16_t check_sum;            //CRC校验，      长度2个字节，开始字节-20
        uint16_t tail;                 //尾，           长度2个字节，开始字节-22
    }data;
}rd_rx_data_t;
#endif

#if (CHUANZHOU_PROTOCOL == 1)
typedef union rd_recv_data
{
    uint8_t buf[14];
    struct
    {
        uint16_t device_id;            //设备ID，            长度2个字节，开始字节-0
        uint8_t  channel;              //通道，              长度1个字节，开始字节-2
        uint8_t  up_down;              //吊臂仰起和下降，     长度1个字节，开始字节-3 
        uint8_t  left_right;           //吊臂左旋和右旋，     长度1个字节，开始字节-4
        uint8_t  oil_control;          //油门控制，          长度1个字节，开始字节-5
        uint8_t  reserve_0;            //保留，              长度1个字节，开始字节-6
        uint8_t  reserve_1;            //保留，              长度1个字节，开始字节-7
        uint8_t  reserve_2;            //保留，              长度1个字节，开始字节-8
        uint8_t  key_0;
        uint8_t  key_1;
        uint8_t  key_2;
        uint8_t  key_3;
        uint8_t  beat_heart;           //心跳包，            长度1个字节
    }data;
}rd_rx_data_t;
extern rd_rx_data_t ub_rd_rx_data;
typedef union rd_send_data
{
    uint8_t buf[14];
    struct  
    {
        uint16_t device_id;            //设备ID，            长度2个字节，开始字节-0
        uint8_t  channel:4;            //通道，              长度1个字节，开始字节-2
        uint8_t  payload_legth:4;      //负载长度，          长度1个字节，开始字节-2
        uint8_t  code;                 //代码，              长度1个字节，开始字节-3
        uint8_t  reserve[10];
    }data;
}rd_tx_data_t;

extern rd_tx_data_t ut_rd_tx_data;
#endif

extern unsigned char   SX1278_RLEN;
typedef struct
{
   void (*lpByteWritefunc)(unsigned char src);
   unsigned char (*lpByteReadfunc)();
   void (*lpSwitchEnStatus)(cmdEntype_t cmd);    //片选
   void (*paSwitchCmdfunc)(cmdpaType_t cmd);     //无线模块，Tx和Rx控制
   void (*lpRecvDataTousr)(rd_rx_data_t *lpbuf,unsigned short length);
}lpCtrlTypefunc_t;
typedef enum 
{
   Sleep_mode	      = (unsigned char)0x00, //sleep mode
   Stdby_mode	      = (unsigned char)0x01, //stbdy mode
   TX_mode 	         = (unsigned char)0x02, //FS mode TX(FSTx)
   Transmitter_mode	= (unsigned char)0x03, //Transmitter mode(Tx)
   RF_mode 		      = (unsigned char)0x04, //FS mode RX(FSRx)
   Receiver_mode	   = (unsigned char)0x05, //Receiver mode(Rx)
   receive_single	   = (unsigned char)0x06,
   CAD_mode		      = (unsigned char)0x07, //Channel activity detection (CAD)
}RFMode_SET;

/*!
 * RegPaConfig
 */
#define RFLR_PACONFIG_PASELECT_MASK                 0x7F 
#define RFLR_PACONFIG_PASELECT_PABOOST              0x80 
#define RFLR_PACONFIG_PASELECT_RFO                  0x00 // Default

#define RFLR_PACONFIG_MAX_POWER_MASK                0x8F

#define RFLR_PACONFIG_OUTPUTPOWER_MASK              0xF0 
typedef enum 
{
   FSK_mode             = (unsigned char)0x00, 
   LORA_mode            = (unsigned char)0x80, 
}  Debugging_fsk_ook;

extern lpCtrlTypefunc_t  ctrlTypefunc;
void register_rf_func(lpCtrlTypefunc_t *func);

void Delay1s(unsigned int ii);
void SX1276WriteBuffer(unsigned char addr, unsigned char data);
void SX1276WriteBuffers( unsigned char addr, unsigned char *buffer,unsigned char size);
unsigned char SX1276ReadBuffer( unsigned char addr);
void SX1276LoRaSetOpMode(RFMode_SET opMode);
void SX1276LoRaFsk(Debugging_fsk_ook opMode);
void SX1276LoRaSetRFFrequency(void);
void SX1276LoRaSetRFPower(int power);
void SX1276LoRaSetSpreadingFactor(unsigned char factor);
void SX1276LoRaSetNbTrigPeaks(unsigned char value);
void SX1276LoRaSetErrorCoding(unsigned char value);
void SX1276LoRaSetPacketCrcOn(bool enable);
void SX1276LoRaSetSignalBandwidth(unsigned char bw);
void SX1276LoRaSetImplicitHeaderOn(bool enable);
void SX1276LoRaSetSymbTimeout(unsigned int value);
void SX1276LoRaSetPayloadLength(unsigned char value);
void SX1276LoRaSetPreamLength(unsigned int value);
void SX1276LoRaSetMobileNode(bool enable);

void FSK_SEND_PACKET(void);
void FUN_RF_SENDPACKET(unsigned char *RF_TRAN_P,unsigned char LEN);
void RF_RECEIVE (void);
void RF_CAD_RECEIVE (void);
void RF_SEELP(void);
void SX1276LORA_INT(void);
void radio_interrupt(void);
void SX1276Reset(void);
op_mode_t SX1276LoRaReadOpMode(void);
void wireless_init(void);
void wireless_rx_data_handle(rd_rx_data_t *pbuf,unsigned short len);

extern uint32_t ul_rd_rx_cnt;
void wireless_tx_data(void);
#endif


